//
//  HSCBannerView.m
//  BannerDemo
//
//  Created by 红沙尘 on 2018/12/14.
//  Copyright © 2018年 红沙尘. All rights reserved.
//

#import "HSCBannerView.h"

#define OFFSET_MAX          ([UIScreen mainScreen].bounds.size.width-64)*0.9


@implementation HSCBannerView {
    CGPoint lastPoint;
    NSMutableArray *viewArray;
    CGFloat off_w;
    CGFloat max_w;//最宽
    CGFloat bili;
    CGFloat offset_left;
}

- (instancetype)initWithFrame:(CGRect)frame
                       offset:(CGFloat)offset
                     dataList:(NSArray *)dataList
                     delegate:(id <HSCBannerViewDelegate>)delegate {
    self = [super initWithFrame:frame];
    
    if (self) {
        self.delegate = delegate;
        self.clipsToBounds = YES;
        offset_left = 0;
        max_w = (frame.size.width - offset*2);
        bili  = frame.size.height/max_w;
        viewArray = [NSMutableArray array];

        CGFloat x = offset;
        for (int i = 0; i < dataList.count; i ++) {
            CGFloat h = frame.size.height;
            CGRect rect = CGRectMake(x, 0, max_w, h);
            if (i == dataList.count - 1) {
                rect.origin.x = offset - offset_left - rect.size.width;
            }
            HSCBannerItemView *itemV = [[HSCBannerItemView alloc] initWithFrame:rect
                                                                           data:dataList[i]];
            x = CGRectGetMaxX(itemV.frame);
            [itemV addTarget:self
                      action:@selector(itemPressed:)
            forControlEvents:UIControlEventTouchUpInside];
            [itemV show];
            [self addSubview:itemV];
            
            [viewArray addObject:itemV];
        }
        
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                              action:@selector(pan:)];
        [self addGestureRecognizer:pan];
    }
    
    return self;
}



- (void)pan:(UIPanGestureRecognizer *)ges {
    CGPoint point = [ges locationInView:self];

    switch (ges.state) {
        case UIGestureRecognizerStateBegan: {
            lastPoint = point;
        }
            break;
        case UIGestureRecognizerStateChanged: {
            CGFloat offsetX = (point.x - lastPoint.x)/3.0;
            [self moveByOffsetX:offsetX];
        }
            break;
        case UIGestureRecognizerStateEnded: {
            CGFloat distance = 1000000.0;
            CGFloat dir = 0;
            for (UIView *v in viewArray) {
                if (v.center.x > self.frame.size.width/2) {
                    if (v.center.x - self.frame.size.width/2 < distance) {
                        distance = v.center.x - self.frame.size.width/2;
                        dir = -1;
                    }
                } else {
                    if (self.frame.size.width/2 - v.center.x < distance) {
                        distance = self.frame.size.width/2 - v.center.x;
                        dir = 1;
                    }
                }
            }
            
            [self moveByOffsetX:distance*dir];
        }
            break;
        default:
            break;
    }
}

- (void)moveByOffsetX:(CGFloat)offsetX {
    for (HSCBannerItemView *v in viewArray) {
        [UIView animateWithDuration:0.3 animations:^{
            v.center = CGPointMake(v.center.x + offsetX, v.center.y);
            [v show];
        }];
    }
    
    CGFloat maxX = 0.0;//找到最右边的view的frame
    CGFloat minX = 1000000;//找到最左边的view的frame
    for (UIView *v in viewArray) {
        if (v.frame.origin.x + v.frame.size.width > maxX) {
            maxX = v.frame.origin.x + v.frame.size.width;
        }
        minX = MIN(v.frame.origin.x, minX);
    }
    
    for (UIView *v in viewArray) {
        if (offsetX < 0) {
            //向左滑动的时候找到滑出屏幕的那个移到最右边
            if (v.frame.origin.x + v.frame.size.width < - 50) {
                CGRect rect = v.frame;
                rect.origin.x = maxX + offset_left;
                v.frame = rect;
                maxX = rect.origin.x + rect.size.width;
            }
        } else {
            //向右滑动的时候找到滑出屏幕的那个移到最左边
            if (v.frame.origin.x > self.frame.size.width + 50) {
                CGRect rect = v.frame;
                rect.origin.x = minX - offset_left - rect.size.width;
                v.frame = rect;
                minX = rect.origin.x;
            }
        }
    }
}

- (void)itemPressed:(HSCBannerItemView *)itemView {
    if ([self.delegate respondsToSelector:@selector(bannerView:didSelectItem:)]) {
        [self.delegate bannerView:self didSelectItem:itemView];
    }
}

@end
